{### Check if this is an object storage policy #}
{% if item[1] is defined %}
{%   set port = item[1]['port'] %}
{%   set type = item[1]['type'] %}
{%   set item = item[0]['policy'] %}
{### If the index is 0 then it needs to be object without index #}
{%   if item.index == 0 %}
{%     set builder_file = type %}
{%   else %}
{%     set builder_file = type + '-' + item.index|string %}
{%   endif %}
{%   set name = item.name %}
{### Otherwise this should be account or container rings #}
{### Make the port/type/item/builder_file/name vals uniform across rings #}
{% elif item.port is defined %}
{%   set port = item.port %}
{%   set type = item.type %}
{%   set item = item.item %}
{%   set builder_file = type %}
{%   set name = type %}
{% endif %}
{### Lets get the min_part_hours, part_power and repl_number vals #}
{% set min_part_hours = item.min_part_hours | default(swift.min_part_hours | default(swift_default_min_part_hours)) %}
{% set part_power = item.part_power | default(swift.part_power) %}
{% if (item.policy_type is defined) and (item.policy_type == "erasure_coding") %}
{%   set repl_number = item.ec_num_data_fragments + item.ec_num_parity_fragments %}
{% else %}
{%   set repl_number = item.repl_number | default(swift.repl_number | default(swift_default_replication_number)) %}
{% endif %}
{### Create the builder dict #}
{% set builder = {} %}
{### This is a hacky way of updating the builder dict #}
{% set _update = builder.update({'min_part_hours':min_part_hours|int}) %}
{% set _update = builder.update({'repl_number':repl_number|int}) %}
{% set _update = builder.update({'part_power':part_power|int}) %}
{% set _update = builder.update({'builder_file':builder_file}) %}
{### Now we need to add the drives #}
{### Create an update the builder dict to have drives as an empty list #}
{% set _update = builder.update({'drives':[]}) %}
{### Lets get the default groups for drives and find the default storage_policy #}
{% set def_groups = [ 'account', 'container' ] %}
{% for policy in swift.storage_policies %}
{%   if policy.policy.default is defined and policy.policy.default == True %}
{%     set _update = def_groups.append(policy.policy.name) %}
{%   endif %}
{% endfor %}
{### Loop through the swift_hosts #}
{% for host in groups['swift_hosts'] %}
{### Default swift_vars to an empty dict if not defined #}
{### swift_vars needs to exist for swift_vars[setting] checks to work #}
{%   if hostvars[host]['swift_vars'] is defined %}
{%     set swift_vars = hostvars[host]['swift_vars'] %}
{%   else %}
{%     set swift_vars = {} %}
{%   endif %}
{### Set the storage and repl ip #}
{%   set storage_ip = hostvars[host]['swift_storage_address'] %}
{%   set repl_ip = hostvars[host]['swift_replication_address'] %}
{### Get the drives use swift global as default #}
{%   set drives = swift_vars.drives | default(swift.drives | default([])) %}
{### Loop through the drives #}
{%   for drive in drives %}
{###   Check if groups is defined per host or drive #}
{%     set groups =  drive.groups | default(swift_vars.groups | default(swift.groups | default(def_groups))) %}
{###   Only build the device if it is part of the group we're building the ring for #}
{%     if name in groups %}
{###     Build an empty device which we'll update with the appropriate details #}
{%       set device = {} %}
{%       set weight = drive.weight | default(swift_vars.weight | default(swift.weight | default(swift_default_drive_weight))) %}
{%       set region = drive.region | default(swift_vars.region | default(swift.region | default(swift_default_host_region))) %}
{%       set zone = drive.zone | default(swift_vars.zone | default(swift.zone | default(swift_default_host_zone))) %}
{###     Update the device with the appropriate values #}
{%       set _update = device.update({'device':drive.name}) %}
{%       set _update = device.update({'weight': weight|int}) %}
{%       set _update = device.update({'region': region|int}) %}
{%       set _update = device.update({'zone': zone|int}) %}
{%       set _update = device.update({'replication_ip': repl_ip}) %}
{%       set _update = device.update({'replication_port': port|int}) %}
{%       set _update = device.update({'ip': storage_ip}) %}
{%       set _update = device.update({'port': port|int}) %}
{###     Append the device to the drives list of the builder dict #}
{%       set _update = builder.drives.append(device) %}
{%     endif %}
{%   endfor %}
{% endfor %}
{### Output the builder file #}
{{ builder | to_nice_json }}
